package com.example.othree.controller;

import com.example.othree.common.R;
import com.example.othree.entity.TowerOzone;
import com.example.othree.entity.TowerOzoneGradientObservations;
import com.example.othree.service.TowerOzoneGradientObservationsService;
import com.example.othree.service.TowerOzoneService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api")
public class TowerOzone2Controller {
    @Autowired
    private TowerOzoneService towerOzoneService;

    @Autowired
    private TowerOzoneGradientObservationsService towerOzoneGradientObservationsService;

    @GetMapping("/gradient-data-now")
    @ApiOperation("获取臭氧梯度观测分钟数据")
    public R getTowerOzone(
            @RequestParam String station,
            @RequestParam String time) {

        Map<String, Object> res= towerOzoneService.getTowerOzoneData(station, time);

        return R.ok().data("data", res);
    }

    @GetMapping("/gradient-data-his")
    @ApiOperation("获取臭氧梯度观测某站点某要素一段时间数据")
    public R getGradientData(
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam String station,
            @RequestParam String element,
            @RequestParam String type
    ){

        // 将 String 转换为 LocalDateTime
        LocalDateTime sTime = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime eTime = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> res = new HashMap<>();
        if("o3".equals(element) || "so2".equals(element) || "co".equals(element) ||"no2".equals(element))
        {
            res = towerOzoneService.getAirQualityData(sTime, eTime, station, element, type);}
        else {
            if(element.equals("wind1")){
                element="Ma_wind";
            }
            if(element.equals("wdir1")){
                element="Ma_wdir";
            }
            if(element.equals("wind2")){
                element="US_wind";
            }
            if(element.equals("wdir2")){
                element="US_wdir";
            }
            if(element.equals("ww2")){
                element="US_W";
            }
            System.out.println("查询梯度风数据");
            System.out.println(element);
            res = towerOzoneService.getWindData(sTime, eTime, station, element, type);
        }

        return R.ok().data("data",res);
    }

    @GetMapping("/gradient-data-all")
    @ApiOperation("获取臭氧梯度观测某站点全部要素一段时间数据")
    public R getAllGradientData(
            @RequestParam String startTime,
            @RequestParam String endTime,
            @RequestParam String stationName,
            @RequestParam String queryType) {

        // 将 String 转换为 LocalDateTime
        LocalDateTime sTime = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime eTime = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        Map<String, Object> res = towerOzoneService.getAirQualityAllData(sTime, eTime, stationName, queryType);
        return R.ok().data("data",res);
    }


}
